package com.css.fxfzypg.modules.rating.library.service;

import cn.hutool.core.util.IdUtil;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.rating.library.dao.KpIndex3DataRepository;
import com.css.fxfzypg.modules.rating.library.model.dto.KpIndex3DataInsertDto;
import com.css.fxfzypg.modules.rating.library.model.dto.KpIndex3DataListDto;
import com.css.fxfzypg.modules.rating.library.model.dto.KpIndex3DataUpdateByIdDto;
import com.css.fxfzypg.modules.rating.library.model.entity.KpIndex3Data;
import com.css.fxfzypg.modules.rating.library.service.cache.KpIndex3DataCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Slf4j
@Service
public class KpIndex3DataService {

    @Autowired
    private KpIndex3DataRepository kpIndex3DataRepository;
    @Autowired
    private KpIndex3DataCacheService kpIndex3DataCacheService;

    public void insert(KpIndex3DataInsertDto dto, String currUid) {
        Date currTime = new Date();
        KpIndex3Data kpIndex3Data = new KpIndex3Data();
        kpIndex3Data.setId(IdUtil.simpleUUID());
        kpIndex3Data.setName(dto.getName());
        kpIndex3Data.setRemark(dto.getRemark());
        kpIndex3Data.setDelFlag(YNEnum.N.toString());
        kpIndex3Data.setIsSystem(0);
        kpIndex3Data.setCreateTime(currTime);
        kpIndex3Data.setCreateUser(currUid);
        kpIndex3Data.setUpdateUser(currUid);
        kpIndex3Data.setUpdateTime(currTime);
        kpIndex3Data.setCode("-1");
        kpIndex3DataRepository.insert(kpIndex3Data);
    }

    public void deleteById(String id, String currUid) {
        String[] ids = id.split(",");
        for (String o : ids) {
            KpIndex3Data kpIndex3Data = kpIndex3DataRepository.getById(o);
            if (kpIndex3Data.getIsSystem() == 1) {
                throw new RuntimeException("系统评分不允许删除");
            }
        }
        kpIndex3DataRepository.deleteById(ids, currUid, new Date());
        kpIndex3DataCacheService.deleteById(ids);
    }

    public void updateById(KpIndex3DataUpdateByIdDto dto, String currUid) throws Exception {
        Date currTime = new Date();
        KpIndex3Data kpIndex3Data = kpIndex3DataRepository.getById(dto.getId());
        if (kpIndex3Data == null) {
            throw new RuntimeException("错误的id" + dto.getId());
        }
        if (kpIndex3Data.getIsSystem() == 1) {
            throw new RuntimeException("系统评分不允许修改");
        }
        kpIndex3Data.setName(dto.getName());
        kpIndex3Data.setRemark(dto.getRemark());
        kpIndex3Data.setUpdateUser(currUid);
        kpIndex3Data.setUpdateTime(currTime);
        kpIndex3DataRepository.updateById(kpIndex3Data);
        kpIndex3DataCacheService.deleteById(kpIndex3Data.getId());
    }

    public Map<String, Object> list(KpIndex3DataListDto dto) {
        return kpIndex3DataRepository.list(dto);
    }

    public KpIndex3Data getById(String id) {
        if (id == null) {
            return null;
        }
        return kpIndex3DataRepository.getById(id);
    }
}
